package cn.digiwin.wx.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class WordToPinYinUtil {

    /**
     * @Description:    汉字转换为拼音
     * @Author:         ganjia
     * @CreateDate:     2020/4/10
     * @Version:        1.0
     */
    public static String ToPinyin(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }
    /**
     * @Description:    汉字转换为拼音首字母
     * @Author:         ganjia
     * @CreateDate:     2020/4/10
     * @Version:        1.0
     */
    public static String ToFirstChar(String str){
        String convert = "";
        for (int j = 0; j <  str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }
    /**
     * @Description:    汉字转换为拼音,首字母大写
     * @Author:         vdi100
     * @CreateDate:     2018/6/23
     * @Version:        1.0
     */
    public static String ToFirstCharUpperCase(String str){
        String convert = "";
        char[] newChar = str.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int j = 0; j <  newChar.length; j++) {
            if (newChar[j] > 128) {
                try {
                    String[] pinyinArray =PinyinHelper.toHanyuPinyinStringArray(newChar[j], defaultFormat);
                    if (pinyinArray != null) {
                        for(int i = 0;i<= pinyinArray[0].length()-1;i++){
                            if(i==0){
                                convert += Character.toString(pinyinArray[0].charAt(0)).toUpperCase();
                            }else {
                                convert += pinyinArray[0].charAt(i);
                            }

                        }

                    } else {
                        convert += newChar[j];
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                convert += newChar[j];
            }
        }
        return convert;
    }
    public static void main(String[] args) {
        System.out.println(ToFirstChar("小生姜").toUpperCase()); //转为首字母大写
        System.out.println(ToPinyin("小生姜"));
        System.out.println(ToFirstCharUpperCase("小生姜")); //转为首字母大写
    }
}
